﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
namespace IHISCETeim.Web.MachineSys.WModel
{
    public partial class SendWPrdctShipLst : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "SendWPrdctShipLst";
            }
        }
        #region 属性 (查询条件)


        protected int PID
        {
            get
            {
                return GetRequestValue<int>("PID", 0);

            }
        }

        protected string U_KW
        {
            get
            {
                return GetRequestValue<string>("U_KW", "");
            }
        }

        protected string AliasName
        {
            get
            {
                return GetRequestValue<string>("AliasName", "");
            }
        }

        protected string ShowAliasName
        {
            get
            {
                return GetRequestValue<string>("ShowAliasName", "");

            }
        }

        protected string NowSendPrdUnit
        {
            get
            {
                return GetRequestValue<string>("NowSendPrdUnit", "0");
            }
        }

        protected string ItemCode
        {
            get
            {
                return GetRequestValue<string>("ItemCode", "");
            }
        }


        private OProjectInfo _pObj = null;
        protected OProjectInfo pObj
        {
            get
            {
                if (_pObj == null)
                    _pObj = OProjectController.GetById(PID);
                return _pObj;
            }
        }

        protected string GetProject()
        {
            DataTable dt = WOWORTaskController.GetNeedSendProject();
            string optiontemp = "<option value='{0}' {2}>{1}</option>";
            string rhtml = "";
            rhtml += string.Format(optiontemp, "0", "请选择", PID == 0 ? "selected" : "", "", "", "");
            foreach (DataRow dr in dt.Rows)
            {
                rhtml += string.Format(optiontemp, dr["PID"], dr["PName"], Convert.ToInt32(dr["PID"].ToString()) == PID ? "selected" : "");
            }
            return rhtml;

        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "search")
            {
                DataBind();
            }
            if (GetRequestValue<string>("ajaxmethod", "") == "addtask")
            {
                addtask();
            }
        }
        
        protected void DataBind()
        {
            //if (PID == 0)
            //    return;
            this.rpMenu.DataSource = WOWORTaskController.GetNeedSendItm(GetSqlWhere());
            this.rpMenu.DataBind();         
           
        }

        protected string GetSqlWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(pObj.PName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("PName", QueryOperatorsEnum.等于, pObj.PName);
                wheresql = sqlcommonFiled;
            }
            string U_KWSql = "";
            if (!string.IsNullOrEmpty(U_KW))
            {
                U_KWSql = QueryOperatorsEnumHelper.ConvertCommonOperator("U_KW", QueryOperatorsEnum.等于, U_KW);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + U_KWSql;
                }
                else
                {
                    wheresql = U_KWSql;
                }
            }

            string AliasNameSQL = "";
            if (!string.IsNullOrEmpty(AliasName))
            {
              //  AliasNameSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("AliasName", QueryOperatorsEnum.等于, AliasName );

                AliasNameSQL = "'" + AliasName + "'";
                AliasNameSQL = AliasNameSQL.Replace(",", "','");
                AliasNameSQL = " AliasName in (" + AliasNameSQL + ")";

                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + AliasNameSQL;
                }
                else
                {
                    wheresql = AliasNameSQL;
                }
            }

            string ItemCodeSQL = "";
            if (!string.IsNullOrEmpty(ItemCode))
            {
                ItemCodeSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("ItemCode", QueryOperatorsEnum.包含, ItemCode);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + ItemCodeSQL;
                }
                else
                {
                    wheresql = ItemCodeSQL;
                }
            }


            return wheresql;
        }


        protected void addtask()
        {

            string IDs = GetRequestValue<string>("id", "");
            string NowSendQtys = GetRequestValue<string>("NowSendQty", "");


            int r = WOWORTaskController.SendPrdctShip(IDs, NowSendQtys,User.RealName);
            if (r <= 0)
                AlertJson("对不起，发料失败");
            else
                SuccessJson("成功发料");

        }

        protected string GetAgentList()
        {
            //DataTable AgentMacStockList = UWarrantyCardController.GetAgentMacNum();

            string litemp = "<li ><a tname=\"{6}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}</a></li>";
            string listr = "";
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetSupplierList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot = AliasName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
                // DataRow[] drs = AgentMacStockList.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
                // string agstock = "0";
                //if (drs.Count() > 0)
                //{
                //    agstock = drs[0]["macnum"].ToString();
                //}
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());

                listr += string.Format(litemp, dr["PYAliasName"].ToString() + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check, agentname);
            }
            return listr;
        }
    }
}